home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / A program 249378192001.psc / ModuleWave.bas < prev    next >
Encoding:
BASIC Source File  |  2001-08-19  |  14.7 KB  |  395 lines

  1. Attribute VB_Name = "ModuleWave"
  2. Option Explicit
  3.  
  4. ' API Declarations
  5. Public Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
  6. Public Enum sndConst
  7.     SND_ASYNC = &H1 ' play asynchronously
  8.     snd_loop = &H8 ' loop the sound until Next sndPlaySound
  9.     SND_MEMORY = &H4 ' lpszSoundName points To a memory file
  10.     SND_NODEFAULT = &H2 ' silence Not default, If sound not found
  11.     SND_NOSTOP = &H10 ' don't stop any currently playing sound
  12.     SND_SYNC = &H0 ' play synchronously (default), halts prog use till done playing
  13. End Enum
  14.  
  15. ' Declare Color Constants
  16. Const cBlue = &HFF0000
  17. Const cRed = &HFF
  18. Const cGreen = &HFF00
  19. Const cYellow = &HFFFF
  20. Const cTeal = &H64FF64
  21. Const cPurple = &HFF007D
  22. Const cGray = &H7D7D7D
  23. Const cOrange = &H96FF
  24.  
  25. ' Define File Header
  26. Public Type FileHeader
  27.     lRiff As String * 4
  28.     lFileSize As Long
  29.     lWave As Long
  30.     lFormat As Long
  31.     lFormatLength As Long
  32.     ' Note: The values in this user defined type MUST follow the above order.
  33. End Type
  34. ' Define First Chunk
  35. Public Type FormatChunk
  36. '    ChunkID As String * 4       ' Chunk ID
  37.     ' Chunk ID is always "fmt "
  38.     wFormatTag As Integer       ' wFormatTag
  39.     ' This code does not support compression.  Therefore, wFormatTag is always one
  40.     
  41.     wChannels As Integer        ' wChannels
  42.     ' wChannels contains the number of audio channels for the sound.
  43.     ' 1 = mono, 2 = Stereo, etc.
  44.         
  45.     dwSamplesPerSec As Long     ' dwSamplesPerSec
  46.     ' dwSamplesPerSec is the sample rate at which the sound is to be
  47.     ' played back in sample frames per second (ie, Hertz).
  48.     
  49.     dwAvgBytesPerSec As Long    ' dwAvgBytesPerSec
  50.     ' dwAvgBytesPerSec field indicates how many bytes play every second
  51.     
  52.     wBlockAlign As Integer      ' wBlockAlign
  53.     ' wBlockAlign is the size of a sample frame, in terms of bytes
  54.     
  55.     wBitsPerSample As Integer    ' wBitsPerSample
  56.     ' wBitsPerSample field indicates the bit resolution of a sample point
  57.     ' (ie, a 16-bit waveform would have wBitsPerSample = 16).
  58.     
  59.     
  60.     ' Note: there may be additional fields here, depending upon wFormatTag.
  61.     ' Note: The values in this user defined type MUST follow the above order.
  62. End Type
  63. ' Define Data Chunks
  64. Public Type ChunkHeader
  65.     ChunkID As String * 4       ' chunkID
  66.     ' Chunk ID is always "data"
  67.     ChunkSize As Long           ' chunkSize
  68.     ' Chunk Size is the number of bytes in the chunk
  69. End Type
  70.  
  71. ' Public Declarations
  72. Public wChunk As ChunkHeader
  73. Public wFileHeader As FileHeader
  74. Public wFormat As FormatChunk
  75.  
  76. Public B_Data() As Byte, LB_Data() As Byte
  77. Public I_Data() As Integer, LI_Data() As Integer
  78.  
  79. Public SStart As Single, SLength As Single  ' Selection values
  80.  
  81. Public dBitsPerTwip As Single    ' For display only
  82.  
  83. Public FileLoaded As Boolean
  84.  
  85. Public Peak As Single, Dip As Single    ' The max and min for the wave
  86.  
  87. Private Temp As Single  ' Dummy value
  88.  
  89.  
  90.  
  91. Public Sub Wave_Display()
  92.     ' Local variable declaration
  93.     Dim CurrentTrack As Integer
  94.         
  95.     Dim I As Single
  96.     
  97.     Dim LoopsCount As Integer
  98.     
  99.     Dim MRatio As Single    ' We will use this value to scale the crest of the wave
  100.                                 ' in WaveForm Window
  101.     Dim GRatio As Single    ' We will use this value to scale the crest of the wave
  102.                                 ' in General Window
  103.     Dim X As Single, Y As Single
  104.     
  105.     ' Lock out controls in form
  106.     With frmMain
  107.         ' Lock out text boxes
  108.         .lblSelStart.Enabled = False
  109.         .txtSelStart.Enabled = False
  110.         .lblSelLength.Enabled = False
  111.         .txtSelLength.Enabled = False
  112.         
  113.         ' Reset text in text boxes
  114.         .txtSelStart.Text = 0
  115.         .txtSelLength.Text = 0
  116.         
  117.         ' Lock out menus
  118.         .mnuFile.Enabled = False
  119.         .mnuHelp.Enabled = False
  120.         
  121.         ' Disable command buttons
  122.         .cmdZoomIn.Enabled = False
  123.         .cmdZoomOut.Enabled = False
  124.         .cmdPlay.Enabled = False
  125.         
  126.         ' Disable check box
  127.         .chkLoop.Enabled = False
  128.         
  129.         ' Disable select functions
  130.         .picGeneralView.Enabled = False
  131.            
  132.         ' Clear .picture box
  133.         .PicWaveForm.Cls
  134.         
  135.         ' Let user know the program is drawing
  136.         .PicWaveForm.Print "Displaying wave sample..."
  137.         DoEvents
  138.     End With
  139.     
  140.     ' Count how many bits are there in a quarter twips
  141.     ' so we can skip them if necessary.
  142.     dBitsPerTwip = (wChunk.ChunkSize / wFormat.wChannels) / (frmMain.PicWaveForm.Width)
  143.     
  144.     ' Make sure dBitsPerTwip is not zero
  145.     Temp = dBitsPerTwip
  146.     If dBitsPerTwip < 1 Then Temp = 1
  147.     
  148.     ' Get ratio (We need the negative so that the wave may draw inverse)
  149.     MRatio = -(frmMain.PicWaveForm.Height / (wFormat.wChannels + 1)) / (Peak - Dip)
  150.     GRatio = -(frmMain.picGeneralView.Height / (wFormat.wChannels + 1)) / (Peak - Dip)
  151.     
  152.     ' Prepare to draw wave form and skip some if necessary.
  153.     With wFormat
  154.         For I = 1 To wChunk.ChunkSize - .wChannels Step .wChannels * Format(dBitsPerTwip, "##000")
  155.             X = X + (frmMain.PicWaveForm.ScaleWidth / (wChunk.ChunkSize / .wBlockAlign) * Format(dBitsPerTwip, "##000"))
  156.             ' Resize Temporary value
  157.             ReDim LI_Data(.wChannels)
  158.             For CurrentTrack = 1 To .wChannels
  159.                 ' Set color
  160.                 Select Case CurrentTrack
  161.                     Case 1
  162.                         frmMain.PicWaveForm.ForeColor = cBlue
  163.                     Case 2
  164.                         frmMain.PicWaveForm.ForeColor = cRed
  165.                     Case 3
  166.                         frmMain.PicWaveForm.ForeColor = cGreen
  167.                     Case 4
  168.                         frmMain.PicWaveForm.ForeColor = cYellow
  169.                     Case 5
  170.                         frmMain.PicWaveForm.ForeColor = cTeal
  171.                     Case 6
  172.                         frmMain.PicWaveForm.ForeColor = cPurple
  173.                     Case 7
  174.                         frmMain.PicWaveForm.ForeColor = cGray
  175.                     Case 8
  176.                         frmMain.PicWaveForm.ForeColor = cOrange
  177.                 End Select
  178.                 ' Draw wave form and invert it so it would display correctly
  179.                 ' Display differently according to its size
  180.                 If dBitsPerTwip > 0.5 Then
  181.                     ' Connect all dots
  182.                     Y = (frmMain.PicWaveForm.Height / (.wChannels + 1) * CurrentTrack)
  183.                     frmMain.PicWaveForm.Line (X, Y + LI_Data(CurrentTrack) * MRatio)- _
  184.                         (X, Y + I_Data(I + CurrentTrack - 1) * MRatio)
  185.                     ' Draw center line
  186.                     If I = 1 Then frmMain.PicWaveForm.Line (0, Y)-(frmMain.PicWaveForm.Width, Y)
  187.                 Else
  188.                     ' No connection between dots
  189.                     Y = (frmMain.PicWaveForm.Height / (.wChannels + 1) * CurrentTrack)
  190.                     frmMain.PicWaveForm.Line (X, Y + I_Data(I + CurrentTrack - 1) * MRatio)- _
  191.                         (X + (frmMain.PicWaveForm.ScaleWidth / wChunk.ChunkSize), _
  192.                         Y + I_Data(I + CurrentTrack - 1) * MRatio), , BF
  193.                     ' Draw center line
  194.                     If I = 1 Then frmMain.PicWaveForm.Line (0, Y)-(frmMain.PicWaveForm.Width, Y)
  195.                 
  196.                 End If
  197.                 
  198.                 ' Draw wave form in General Window
  199.                     frmMain.picGeneralView.ForeColor = frmMain.PicWaveForm.ForeColor
  200.                     Y = (frmMain.picGeneralView.Height / (.wChannels + 1) * CurrentTrack)
  201.                     frmMain.picGeneralView.Line (X, Y + LI_Data(CurrentTrack) * GRatio)- _
  202.                         (X, Y + I_Data(I + CurrentTrack - 1) * GRatio)
  203.                     
  204.                     ' Draw center line
  205.                     If I = 1 Then frmMain.picGeneralView.Line (0, Y)-(frmMain.picGeneralView.Width, Y)
  206.                 
  207.                 ' Update LI_Data
  208.                 LI_Data(CurrentTrack) = I_Data(I + CurrentTrack - 1)
  209.             Next CurrentTrack
  210.             
  211.             ' Update display every two hundred fifty loop
  212.             If LoopsCount >= 250 Then
  213.                 DoEvents
  214.                 LoopsCount = 0
  215.             Else
  216.                 LoopsCount = LoopsCount + 1
  217.             End If
  218.         
  219.             ' Regardless of outcome, stop drawing any further if X is greater than
  220.             ' the width of the window
  221.             If X > frmMain.picGeneralView.ScaleWidth Then Exit For
  222.         Next I
  223.     End With
  224.     
  225.     ' Enable previously disabled controls
  226.     With frmMain
  227.         ' Make frmmain.picGeneralView's wave sample ineffect to Cls
  228.         .picGeneralView.Picture = .picGeneralView.Image
  229.                         
  230.         ' Unlock text boxes
  231.         .lblSelStart.Enabled = True
  232.         .txtSelStart.Enabled = True
  233.         .lblSelLength.Enabled = True
  234.         .txtSelLength.Enabled = True
  235.         
  236.         ' Disable check box
  237.         .chkLoop.Enabled = True
  238.         
  239.         ' Unlock menus
  240.         .mnuFile.Enabled = True
  241.         .mnuHelp.Enabled = True
  242.         
  243.         ' Enable command button
  244.         .cmdPlay.Enabled = True
  245.         
  246.         ' Enable zoom function
  247.         .picGeneralView.Enabled = True
  248.         
  249.         ' Loading complete.  Cover previous message.
  250.         .PicWaveForm.ForeColor = .PicWaveForm.BackColor
  251.         .PicWaveForm.CurrentX = 0: .PicWaveForm.CurrentY = 0
  252.         .PicWaveForm.Print "Displaying wave sample..."
  253.         
  254.         ' Reset forecolor
  255.         .PicWaveForm.ForeColor = vbBlack
  256.     End With
  257. End Sub
  258.  
  259. Public Sub Wave_EnlargeView(BStart As Single, BLength As Single)
  260.     ' Local variable declaration
  261.     Dim bLengthPerTwip As Single
  262.     
  263.     Dim CurrentTrack As Integer
  264.         
  265.     Dim I As Single
  266.     
  267.     Dim LoopsCount As Integer
  268.     
  269.     Dim MRatio As Single    ' We will use this value to scale the crest of the wave
  270.                                 ' in WaveForm Window
  271.  
  272.     Dim X As Single, Y As Single
  273.     
  274.                     
  275.     ' Reset values
  276.     ReDim LB_Data(wFormat.wChannels)
  277.     ReDim LI_Data(wFormat.wChannels)
  278.     
  279.     
  280.     ' Lock out controls
  281.     With frmMain
  282.         ' Lock out menus
  283.         .mnuFile.Enabled = False
  284.         .mnuHelp.Enabled = False
  285.         
  286.         ' Clear .picture box
  287.         .PicWaveForm.Cls
  288.         
  289.         ' Let user know the program is drawing
  290.         .PicWaveForm.Print "Displaying wave sample..."
  291.         DoEvents
  292.     End With
  293.     
  294.     ' Count how many bits are there in a quarter twips
  295.     ' so we can skip them if necessary.
  296.     bLengthPerTwip = (BLength * wFormat.wChannels) / frmMain.PicWaveForm.Width
  297.     
  298.     ' Make sure dBitsPerTwip is not zero
  299.     Temp = bLengthPerTwip
  300.     If Temp < 1 Then Temp = 1
  301.     
  302.     ' Get ratio
  303.     MRatio = -(frmMain.PicWaveForm.Height / (wFormat.wChannels + 1)) / (Peak - Dip)
  304.     
  305.     ' Prepare to draw wave form
  306.     With wFormat
  307.         For I = Int(BStart * .wChannels) To Int(BStart + BLength) * .wChannels - .wChannels _
  308.             Step Int(Temp * .wChannels)
  309.             ' Increase X
  310.             X = X + frmMain.PicWaveForm.ScaleWidth / (BLength / Temp)
  311.             For CurrentTrack = 1 To .wChannels
  312.                 ' Set color
  313.                 Select Case CurrentTrack
  314.                     Case 1
  315.                         frmMain.PicWaveForm.ForeColor = cBlue
  316.                     Case 2
  317.                         frmMain.PicWaveForm.ForeColor = cRed
  318.                     Case 3
  319.                         frmMain.PicWaveForm.ForeColor = cGreen
  320.                     Case 4
  321.                         frmMain.PicWaveForm.ForeColor = cYellow
  322.                     Case 5
  323.                         frmMain.PicWaveForm.ForeColor = cTeal
  324.                     Case 6
  325.                         frmMain.PicWaveForm.ForeColor = cPurple
  326.                     Case 7
  327.                         frmMain.PicWaveForm.ForeColor = cGray
  328.                     Case 8
  329.                         frmMain.PicWaveForm.ForeColor = cOrange
  330.                 End Select
  331.                 ' Draw wave form and invert it so it would display correctly
  332.                 If bLengthPerTwip > 0.25 Then
  333.                     ' Connect all dots
  334.                     Y = (frmMain.PicWaveForm.Height / (.wChannels + 1) * CurrentTrack)
  335.                     frmMain.PicWaveForm.Line (X, Y + LI_Data(CurrentTrack) * MRatio)- _
  336.                         (X, Y + I_Data(I + CurrentTrack - 1) * MRatio)
  337.                     ' Draw center line
  338.                     frmMain.PicWaveForm.ForeColor = vbBlack
  339.                     If I = (BStart * .wChannels) Then frmMain.PicWaveForm.Line (0, Y)-(frmMain.PicWaveForm.Width, Y)
  340.                 Else
  341.                     ' No connection between dots
  342.                     Y = (frmMain.PicWaveForm.Height / (.wChannels + 1) * CurrentTrack)
  343.                     frmMain.PicWaveForm.Line (X, Y + I_Data(I + CurrentTrack - 1) * MRatio)- _
  344.                         (X + (frmMain.PicWaveForm.ScaleWidth / BLength), _
  345.                         Y + I_Data(I + CurrentTrack - 1) * MRatio), , BF
  346.                     ' Draw center line
  347.                     frmMain.PicWaveForm.ForeColor = vbBlack
  348.                     If I = (BStart * .wChannels) Then frmMain.PicWaveForm.Line (0, Y)-(frmMain.PicWaveForm.Width, Y)
  349.                 
  350.                 End If
  351.                             
  352.                 ' Update LI_Data
  353.                 LI_Data(CurrentTrack) = I_Data(I + CurrentTrack - 1)
  354.             Next CurrentTrack
  355.             
  356.             ' Update display every two hundred fifty loop
  357.             If LoopsCount >= 250 Then
  358.                 DoEvents
  359.                 LoopsCount = 0
  360.             Else
  361.                 LoopsCount = LoopsCount + 1
  362.             End If
  363.         
  364.             ' Regardless of outcome, stop drawing any further if X is greater than
  365.             ' the width of the window
  366.             If X > frmMain.picGeneralView.ScaleWidth Then Exit For
  367.         Next I
  368.     End With
  369.                     
  370.     ' Enable previously disabled controls
  371.     With frmMain
  372.         ' Unlock text boxes
  373.         .lblSelStart.Enabled = True
  374.         .txtSelStart.Enabled = True
  375.         .lblSelLength.Enabled = True
  376.         .txtSelLength.Enabled = True
  377.         
  378.         ' Unlock menus
  379.         .mnuFile.Enabled = True
  380.         .mnuHelp.Enabled = True
  381.         
  382.         ' Enable command button
  383.         .cmdPlay.Enabled = True
  384.         
  385.         ' Loading complete.  Cover previous message.
  386.         .PicWaveForm.ForeColor = .PicWaveForm.BackColor
  387.         .PicWaveForm.CurrentX = 0: .PicWaveForm.CurrentY = 0
  388.         .PicWaveForm.Print "Displaying wave sample..."
  389.         
  390.         ' Reset forecolor
  391.         .PicWaveForm.ForeColor = vbBlack
  392.     End With
  393. End Sub
  394.  
  395.